<template>
    <var-action-sheet
        :actions="actions"
        v-model:show="dialogStore.isPayDialogVisible"
        @select="onSelect"
    />
</template>
<script setup>
import { GetVipPay, GetTemplatePay } from "@/api/index";

const dialogStore = useDialogStore();
const userStore = useUserStore();
const orderStore = useOrderStore();
const pageStore = usePageStore();
const actions = [
    {
        name: "支付宝",
        icon: `${_oss_}images/mobile/icon-alipay.png`,
        color: "#1296db",
        type: "alipay",
    },
    {
        name: "微信",
        icon: `${_oss_}images/mobile/icon-weixin.png`,
        color: "#0ABC64",
        type: "weixin",
    },
];
let pay_channel = null;

const onPay = async ({ pay_channel }) => {
    let apiFunc = { 1: GetVipPay, 2: GetTemplatePay }[orderStore.order_type];
    let params = {
        pay_channel,
        order_no: orderStore.order_no,
        code: pageStore.code,
        openid: userStore.openid,
    };
    orderStore.order_type === 1
        ? (params["vip_type"] = orderStore.vip_type)
        : (params = {
              ...params,
              ...{
                  template_id: orderStore.template_id,
                  temspecs_id: orderStore.temspecs_id,
                  temspecs_type: orderStore.temspecs_type,
                  redirect_url:
                      pageStore.isMobile && !IsWeiXin() ? location.href : "",
              },
          });
    let res = await apiFunc(params);
    if (res.code == 200) {
        const { order_no, content } = res.data;
        orderStore.order_no = order_no;
        if (content.openid) userStore.openid = content.openid;
        if (pay_channel === 1) {
            if (!IsWeiXin()) {
                window.location.href = content;
            } else {
                WeixinJSBridge.invoke(
                    "getBrandWCPayRequest",
                    content,
                    function (res) {
                        if (res.err_msg == "get_brand_wcpay_request:ok") {
                            Message().success("支付成功");
                            setTimeout(function () {
                                location.reload();
                            }, 1500);
                        } else if (
                            res.err_msg == "get_brand_wcpay_request:cancel"
                        ) {
                            Message().info("您取消了支付");
                        } else if (
                            res.err_msg == "get_brand_wcpay_request:fail"
                        ) {
                            Message().error("您取消了支付");
                        } else {
                            Message().warning(
                                typeof res.err_msg != "undefined"
                                    ? res.err_msg
                                    : typeof res.errMsg !== "undefined"
                                    ? res.errMsg
                                    : "未知支付状态"
                            );
                        }
                    }
                );
            }
        } else {
            alipayForm(content);
            dialogStore.isOrderStatusDialogVisible = true;
        }
    } else {
        dialogStore.isPayDialogVisible = false;
        Message().error(res.msg);
    }
};

const onSelect = (action) => {
    if (action.type === "weixin") {
        pay_channel = 1;
        onPay({ pay_channel });
    } else if (action.type === "alipay") {
        pay_channel = 2;
        onPay({ pay_channel });
    }
};
</script>
